package com.banmao.mall.service.client.impl;

import com.banmao.mall.mapper.OrderMapper;
import com.banmao.mall.mapper.UserMapper;
import com.banmao.mall.model.vo.client.login.*;
import com.banmao.mall.service.client.ClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("cClientServiceImpl")
public class ClientServiceImpl implements ClientService {

    @Autowired
    UserMapper userMapper;
    @Autowired
    OrderMapper orderMapper;

    @Override
    public Client login(String username) {

        Client userLogin = userMapper.selectByUsername(username);

        return userLogin;
    }

    @Override
    public ClientLoginVO info(Integer userId) {

        // 获取avatarUrl和nickName的值
        UserInfoVO userInfoVO = userMapper.selectAvatarAndNickname(userId);

        ClientLoginVO clientLoginVO = new ClientLoginVO();
        clientLoginVO.setUserInfo(userInfoVO);

        return clientLoginVO;
    }

    @Override
    public UserIndexVO userIndex(Integer userId) {

        // 根据用户ID查订单状态集合
        List<Integer> orderStatusList = orderMapper.selectStatsByUerID(userId);

        Integer uncomment = 0; // 未评价个数
        Integer unpaid = 0;  // 未付款个数
        Integer unrecv = 0;  // 未收货个数
        Integer unship = 0;  // 未发货个数

        // 遍历集合
        for (Integer orderStatus : orderStatusList) {
            if (orderStatus != null) {
                if(orderStatus == 101) {
                    // 101未付款
                    unpaid++;
                } else if(orderStatus == 201) {
                    // 201未发货
                    unship++;
                } else if(orderStatus == 301) {
                    // 301未收货
                    unrecv++;
                } else if(orderStatus == 401) {
                    // 401未评价
                    uncomment++;
                }
            }
        }

        UserIndexOrderVO userIndexOrderVO = new UserIndexOrderVO();
        userIndexOrderVO.setUncomment(uncomment);
        userIndexOrderVO.setUnpaid(unpaid);
        userIndexOrderVO.setUnrecv(unrecv);
        userIndexOrderVO.setUnship(unship);

        UserIndexVO userIndexVO = new UserIndexVO();
        userIndexVO.setOrder(userIndexOrderVO);

        return userIndexVO;
    }
}
